package models

import (
	"time"

	"gorm.io/gorm"
)

// MaRedemptionCode [...]
type MaRedemptionCode struct {
	ID                     string    `gorm:"primaryKey;column:id;type:bigint unsigned;not null;comment:设备权限ID"  json:"id" json_old:"ID"`
	MerchantID             string    `gorm:"primaryKey;column:merchant_id;type:varchar(30);not null;comment:商户id" json:"merchant_id"`
	RedemptionCode         string    `gorm:"primaryKey;column:redemption_code;type:varchar(50);not null;comment:兑换码内容"  json:"redemption_code" json_old:"RedemptionCode"`
	Value                  float64   `gorm:"primaryKey;column:value;type:decimal(10,2);not null;comment:兑换码价值"  json:"value" json_old:"Value"`
	ExchangeValue          float64   `gorm:"column:exchange_value;type:decimal(10,2);default:null;comment:兑换商品价值"  json:"exchange_value" json_old:"ExchangeValue"`
	SalesID                string    `gorm:"column:sales_id;type:varchar(50);default:null;comment:售货识别ID 序列号"  json:"sales_id" json_old:"SalesID"`
	RobotName              string    `gorm:"column:robot_name;type:varchar(50);default:null;comment:机器人名称"  json:"robot_name" json_old:"RobotName"`
	ClassID                string    `gorm:"column:class_id;type:bigint;default:null;comment:类目ID"  json:"class_id" json_old:"ClassID"`
	GoodsID                string    `gorm:"column:goods_id;type:bigint;default:null;comment:商品ID"  json:"goods_id" json_old:"GoodsID"`
	OutTradeNo             string    `gorm:"column:out_trade_no;type:varchar(50);default:null;comment:订单编号"  json:"out_trade_no" json_old:"OutTradeNo"`
	AddTime                time.Time `gorm:"column:add_time;type:datetime;default:null;comment:添加时间"  json:"-" json_old:"-"`
	AddTimeFormated        string    `gorm:"-" json:"add_time" json_old:"AddTime"`
	UsageTime              time.Time `gorm:"column:usage_time;type:datetime;default:null;comment:使用时间"  json:"-" json_old:"-"`
	UsageTimeFormated      string    `gorm:"-" json:"usage_time" json_old:"UsageTime"`
	ExpirationTime         time.Time `gorm:"primaryKey;column:expiration_time;type:datetime;not null;comment:失效时间"  json:"-" json_old:"-"`
	ExpirationTimeFormated string    `gorm:"-" json:"expiration_time" json_old:"ExpirationTime"`
	RedemptionState        int       `gorm:"primaryKey;column:redemption_state;type:int;not null;comment:0-失效 1-未使用 2-已使用"  json:"redemption_state" json_old:"RedemptionState"`
	BrandName              string    `gorm:"column:brand_name;type:varchar(50);default:null;comment:品牌方名称"  json:"brand_name" json_old:"BrandName"`
	BatchID                string    `gorm:"column:batch_id;type:varchar(30);not null;default:'';comment:批次id" json:"batch_id"`
	CreatedAt              int64     `gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
	NewExpirationTime      int64     `gorm:"column:new_expiration_time;type:int;size:64" json:"new_expiration_time"`
	NewUsageTime           int64     `gorm:"column:new_usage_time;type:int;size:64" json:"new_usage_time"`
}

func (m *MaRedemptionCode) AfterFind(tx *gorm.DB) (err error) {
	if !m.AddTime.IsZero() {
		AddTimeByte, _ := DateTime(m.AddTime).MarshalDateTime()
		m.AddTimeFormated = string(AddTimeByte)
	}
	if !m.UsageTime.IsZero() {
		UsageByte, _ := DateTime(m.UsageTime).MarshalDateTime()
		m.UsageTimeFormated = string(UsageByte)
	}
	if !time.Time(m.ExpirationTime).IsZero() {
		ExpirationTimeByte, _ := DateTime(m.ExpirationTime).MarshalDateTime()
		m.ExpirationTimeFormated = string(ExpirationTimeByte)
	}
	return
}

// TableName get sql table name.获取数据库表名
func (m MaRedemptionCode) TableName() string {
	return "ma_redemption_code"
}

// 模式自动注册
func init() {
	Models = append(Models, &MaRedemptionCode{})
}
